package cn.bob.order.controller;

import cn.bob.basic.jwt.LoginContext;
import cn.bob.order.dto.OrderDto;
import cn.bob.order.service.IOrderProductService;
import cn.bob.order.domain.OrderProduct;
import cn.bob.order.query.OrderProductQuery;
import cn.bob.basic.utils.AjaxResult;
import cn.bob.basic.utils.PageList;
import cn.bob.org.domain.Employee;
import cn.bob.org.mapper.EmployeeMapper;
import cn.bob.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/orderProduct")
public class OrderProductController {
    @Autowired
    public IOrderProductService orderProductService;
    @Autowired
    private EmployeeMapper employeeMapper;
    /**
     * 保存和修改公用的
     * @param orderProduct  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderProduct orderProduct){
        try {
            if( orderProduct.getId()!=null)
                orderProductService.update(orderProduct);
            else
                orderProductService.add(orderProduct);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            orderProductService.delById(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public OrderProduct get(@PathVariable("id")Long id)
    {
        return orderProductService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<OrderProduct> list(){

        return orderProductService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<OrderProduct> json(@RequestBody OrderProductQuery query,HttpServletRequest request)
    {   //有shopid的用户只能查询自己店铺的订单，没有就是平台用户可以查询所有订单
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee byLogid = employeeMapper.findByLogid(logininfo.getId());
        if (byLogid.getShopId()!=null){
            query.setShopId(byLogid.getShopId());
        }
        return orderProductService.queryByPage(query);
    }

    /**
     * 查看所有的员工信息
     * @return
     */
    @PostMapping("/confirm")
    public AjaxResult confirm(@RequestBody OrderDto orderDto) {
        orderProductService.confirm(orderDto);
        return AjaxResult.me().success();
    }

    /**
     * 查看所有的员工信息
     * @return
     */
    @GetMapping("/cancel/{id}")
    public AjaxResult cancel(@PathVariable("id")Long id) {
        orderProductService.cancel(id);
        return AjaxResult.me().success();
    }
}
